package com.maizhi.controller;

import java.io.*;
import java.util.List;
import java.util.ResourceBundle;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.maizhi.httpmodel.Json;
import com.maizhi.model.Files;
import com.maizhi.service.FilesServiceI;
import com.maizhi.util.uploadSpringSinaUtil;
import com.maizhi.util.uploadSpringUtil;
import com.sina.sae.storage.SaeStorage;
import com.sina.sae.util.SaeUserInfo;
 


@Controller
@RequestMapping("/sinaController")
public class sinaController extends BaseController {

	Logger logger = Logger.getLogger(sinaController.class);
	
	private String accessKey;
    private String secretKey;
    private String appName;
    private String seaTempPath;
    private String domain;
		
    FilesServiceI fileService;
    
    public FilesServiceI getFileService() {
		return fileService;
	}

	@Autowired
	public void setFileService(FilesServiceI fileService) {
		this.fileService = fileService;
	}
	
	public sinaController(){
		ResourceBundle config = ResourceBundle.getBundle("config");
		accessKey = config.getString("accessKey");
		secretKey = config.getString("secretKey");
		appName   = config.getString("appName");
		seaTempPath=config.getString("seaTempPath");
		domain    = config.getString("domain");
	}
	
	
	@RequestMapping(params = "upload")
	public ModelAndView uplaod(){
		ModelAndView model = new ModelAndView("upload2");
		
		uploadSpringSinaUtil ussu = new uploadSpringSinaUtil();
		List<Files> files = ussu.upload(request, response);
		logger.info("  files :  "+files);
		if(files!=null && files.size()>0){
			for(Files file : files){
				logger.debug("fileName: "+file.getFileName());
				logger.debug("filePath: "+file.getFilePath());
				logger.debug("fileSuffix: "+file.getFileSubffix());
				fileService.save(file);
			}
		}
		
		model.addObject("list", files);
		
		return model;
	}
	
	
	@RequestMapping(params = "list")
	public ModelAndView list(){
		ModelAndView model = new ModelAndView("upload2_list");
		
		SaeStorage ss = new SaeStorage(accessKey,secretKey,appName);
		
		logger.info("getListByPath(domain) : "+ss.getListByPath(domain));

		List<String> list = ss.getList(domain);
		if(list!=null && list.size()>0){
			for(int m=0;m<list.size();m++){
				logger.info("domain file url : "+ss.getUrl(domain,list.get(m)));
			}
		}
		model.addObject("list", list);
		return model;
	}
	
	@RequestMapping(params = "down")
	@ResponseBody
	public Json down(Files file){
		Json json = new Json();
		Files files = (Files) fileService.get(Files.class,file.getId());
		if(files!=null){
			uploadSpringSinaUtil ussu = new uploadSpringSinaUtil();
			try {
				String dmsg = ussu.dowload(files, request, response);
				json.setMsg(dmsg);
				json.setSuccess(true);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			json.setMsg("数据库找不到相应文件");
		}
		return json;
	}
}
